var app = getApp();
const util = require('../../utils/util.js')
const requireHttps = app.globalData.requireHttps //获取require域名
Page({
  data: {
    allMoney: '',
    balance: ''
  },
  onLoad: function() {
    let _this = this;
    _this.getCashDetail()
    //获取用户余额
    _this.setData({
      balance: app.globalData.balance,
      cashMoney: ''
    })
  },
  //全部提现
  allCarryCash: function(e) {
    let _this = this;
    //用户提现金额为0或空时
    if (app.globalData.balance === '0.00' || app.globalData.balance == undefined || app.globalData.balance == null) {
      wx.showModal({
        title: '提示',
        content: '提现金额不能为0.00或是空',
        success: res => {
          if (res.confirm) {
            console.log('用户选择确定')
          } else if (res.cancel) {
            cosole.log('用户选择取消')
          }
        }
      })
    } else {
      _this.setData({
        allMoney: app.globalData.balance,
      })
    }
  },
  //提现金额
  cashMoney: function(e) {
    app.globalData.cashMoney = e.detail.value;
    console.log(app.globalData.cashMoney);
  },
  //用户点击提现按钮
  carryCash: util.throttle(function() {
    let _this = this;
    // console.log(app.globalData.cashMoney);

    // console.log(_this.data.allMoney);
    let balance = app.globalData.balance.split('.');
    // console.log(balance[0]);
    //全部提现按钮
    if (app.globalData.balance == _this.data.allMoney) {
      // console.log(app.globalData.balance);
      //获取用户余额
      let cashMoney = app.globalData.balance.split('.')
      console.log(cashMoney[0]);
      let cashMoneys = app.globalData.cashMoney.split('.')
      if (cashMoneys[0] == '' || cashMoneys[0] == null || cashMoneys[0] == undefined) {
        console.log('全部提现')
        wx.request({
          url: '' + requireHttps + '/api/member/userWithdrawal',
          data: {
            token: app.globalData.token,
            money: cashMoney[0]
          },
          method: 'post',
          success: res => {
            wx.showToast({
              title: '提现成功',
              success: res => {
                //获取用户余额
                cashMoney = ''
                _this.setData({
                  balance: app.globalData.balance,
                  allMoney: ''
                })
                wx.navigateBack({
                  delta: 1
                })
              }
            })
          }
        })
      } else {
        console.log(cashMoneys[0]+'提现用户输入金额')
        wx.request({
          url: '' + requireHttps + '/api/member/userWithdrawal',
          data: {
            token: app.globalData.token,
            money: cashMoneys[0]
          },
          method: 'post',
          success: res => {
            wx.showToast({
              title: '提现成功',
              success: res => {
                //获取用户余额
                cashMoneys = ''
                _this.setData({
                  balance: app.globalData.balance,
                  allMoney: ''
                })
                wx.navigateBack({
                  delta: 1
                })
              }
            })
          }
        })
      }
    } else if (_this.data.allMoney == '' && app.globalData.cashMoney == '') {
      wx.showModal({
        title: '提示',
        content: '请输入你需要提现的金额',
        success: res => {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else if (parseInt(app.globalData.cashMoney) > parseInt(balance[0])) {
      // console.log(app.globalData.cashMoney + '---' + balance[0])
      wx.showModal({
        title: '提示',
        content: '提现金额不能大于余额',
        success: res => {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      // console.log(app.globalData.cashMoney)
      //获取用户输入的金额
      let cashMoney = app.globalData.cashMoney.split('.')
      //当用户输入的金额有小数时
      if (cashMoney[1] > 0) {
        wx.showModal({
          title: '提示',
          content: '提现金额不能含有小数',
          success: res => {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      } else { //调用提现接口
        //用户输入金额
        // let allMoney = app.globalData.cashMoney
        // console.log(cashMoney)
        wx.request({
          url: '' + requireHttps + '/api/member/userWithdrawal',
          data: {
            token: app.globalData.token,
            money: app.globalData.cashMoney
          },
          method: 'post',
          success: res => {
            wx.showToast({
              title: '提现成功',
              success: res => {
                app.globalData.cashMoney = ''
                //获取用户余额
                _this.setData({
                  balance: app.globalData.balance,
                  cashMoney: '',
                })
                wx.navigateBack({
                  delta: 1
                })
              }
            })
          }
        })
      }
    }
  },1000),
  getCashDetail: function() {
    let _this = this
    wx.request({
      url: '' + requireHttps + '/api/publics/getSystemConfig',
      data: {},
      method: 'post',
      success: res => {
        _this.setData({
          cashDetail: res.data.data
        })
      }
    })
  }
})